package ewu.expressions

import ewu._
import scala.collection.mutable.ArrayBuffer

/**
 * Diese Klasse stellt einer Arrayzugriff auf das Array "name" 
 * ueber einen Key "index" dar.
 * Beispiel: A(i)
 */

case class ArrayAtIndex(override val name: String, index: Expression) extends Name(name) {
  override def evaluate(env: Environment): Option[BigInt] = {
    env.getValue(name) match {
      case Some(x: ExpArray) => 
        return x.valueAt(index.evaluate(env) match {
          case Some(x) => x.toInt
          case None => throw new IllegalArgumentException(
          "Index not evaluable: " + index.toString)
      }).evaluate(env)
      case Some(x) => None
      case None => return None
    }
  }
  
  override def toString: String = name + "(" + index + ")"
  
  override def getVariables(a: ArrayBuffer[Name]) = {
    if(!a.contains(this))
      a.append(this)
    index.getVariables(a)
  }
}
